/*
 *  utilTester.cpp
 *  SoundController0002
 *
 *  Created by jiro on 11/09/05.
 *  Copyright 2011 jiro music. All rights reserved.
 *
 */

#include "utilTester.h"
#include "../composition 0039 util.h"
#include "../../../sc/Tester/CustomAssert.h"
#include "../../../sc/SoundController.h"

using namespace sc;
using namespace sc::hs;

namespace theory0039
{
	void utilTester::TestAll()
	{
		Test_SequenceElement();
		Test_Section();
	}

	void utilTester::Test_SequenceElement()
	{
		SequenceElement se( Note::A4, 1, Note::A4, 1.01, Note::mezzoForte, 1 );
		double howlFreq = 444.4 - 440;
		AssertEqual( se.GetFrequency( 0 ), WellTemperament::GetFrequency( Note::A4 ) );
		AssertEqual( se.GetFrequency( 1 ), 1.01 * WellTemperament::GetFrequency( Note::A4 ) );
		AssertEqual( se.GetRepeatCount(), 1 );
		AssertEqualWithError( se.GetDuration( true ), 1.0 / howlFreq, 0.0000001 );

		se.SetRepeatCount( 3 );

		AssertEqual( se.GetRepeatCount(), 3 );
		AssertEqualWithError( se.GetDuration( true ), 3.0 / howlFreq, 0.0000001 );
	}

	void utilTester::Test_Section()
	{
		SequenceElement base( Note::A4, 1, Note::A4, 1.01, Note::mezzoForte, 1 );		//	440, 440 + 4.4

		Section s( base, 1 );
		s.AddSubElement( SequenceElement( Note::A5, 1, Note::A5, 1.01, Note::mezzoForte, 1 ), 0 );	// 880, 880 + 8.8
		s.AddSubElement( SequenceElement( Note::A6, 1, Note::A6, 1.025, Note::mezzoForte, 1 ), 0 );	// 1760, 1760 + 44

		AssertEqualWithError( s.GetMaxSubElementDuration(), 1.0 / 8.8, 0.000001 );
		AssertEqual( s.GetMinRepeatCountOfBaseElement(), 1 );
		s.SetRepeatCountOfBaseElement( 1 );
		AssertEqual( s.GetBaseElement().GetRepeatCount(), 1 );

		s.AddSubElement( SequenceElement( Note::A5, 1, Note::A5, 1.00125, Note::mezzoForte, 1 ), 0 ); // 880, 880 + 1.1
		AssertEqualWithError( s.GetMaxSubElementDuration(), 1.0 / 1.1, 0.000001 );
		AssertEqual( s.GetMinRepeatCountOfBaseElement(), 4 );
		s.SetRepeatCountOfBaseElement( s.GetMinRepeatCountOfBaseElement() );
		AssertEqual( s.GetBaseElement().GetRepeatCount(), 4 );

	}

}
